// File: jmcomic-core/src/main/java/dev/jukomu/common/postman/proxy/PostmanProxy.java
package dev.jukomu.common.postman.proxy;

import dev.jukomu.common.postman.Postman;
import dev.jukomu.common.util.net.IResponse;

import java.util.Map;
import java.util.Objects;

/**
 * 对应Python的PostmanProxy，Postman的抽象代理类。
 * 转发所有Postman接口方法到内部的Postman实例。
 */
public abstract class PostmanProxy implements Postman {

    protected final Postman postman; // 内部被代理的Postman实例

    /**
     * 构造函数。
     *
     * @param postman 被代理的Postman实例。
     */
    public PostmanProxy(Postman postman) {
        this.postman = Objects.requireNonNull(postman, "Postman to be proxied cannot be null.");
    }

    @Override
    public IResponse get(String url, Map<String, Object> kwargs) throws Exception {
        return postman.get(url, kwargs);
    }

    @Override
    public IResponse post(String url, Map<String, Object> kwargs) throws Exception {
        return postman.post(url, kwargs);
    }

    @Override
    public Postman copy() {
        // 代理的copy方法应该复制内部Postman，并用新的内部Postman构造新的代理实例
        // 具体实现由子类完成，因为子类需要自己的构造函数来创建代理实例
        throw new UnsupportedOperationException("Subclasses must implement copy() method.");
    }

    @Override
    public <T> T getMetaData(String key, T defaultValue) {
        return postman.getMetaData(key, defaultValue);
    }

    @Override
    public Map<String, Object> getMetaData() {
        return postman.getMetaData();
    }

    @Override
    public Object getItem(String key) {
        return postman.getItem(key);
    }

    @Override
    public void setItem(String key, Object value) {
        postman.setItem(key, value);
    }

    @Override
    public Postman getRootPostman() {
        // 递归获取最底层的Postman实例
        return postman.getRootPostman();
    }

    // --- 代理方法链式调用，在此代理层实现转发 ---

    @Override
    public Postman withFixUrl(String url) {
        return postman.withFixUrl(url);
    }

    @Override
    public Postman withRetry(int retryTimes, Class<? extends Postman> clazz) {
        return postman.withRetry(retryTimes, clazz);
    }

    @Override
    public Postman withMultiPart() {
        return postman.withMultiPart();
    }

    @Override
    public Postman withWrapResp(Class<? extends IResponse> clazz) {
        return postman.withWrapResp(clazz);
    }

    @Override
    public Postman withRedirectCatching(Class<? extends Postman> clazz) {
        return postman.withRedirectCatching(clazz);
    }
}